package com.lyy.mapper;

import com.lyy.entity.Chapter;
import com.lyy.entity.Episode;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Repository
public interface EpisodeMapper {

    String list = "select e.episode_id, e.episode_title, e.episode_info, e.episode_url, e.episode_index, " +
            "e.fk_chapter_id, e.create_time, e.modify_time from lesson.episode e ";

    /**
     * 按条件查询集记录
     *
     * @param condition 查询条件
     * @return 查询失败时返回空List对象，结果集按 episode_index 升序，episode_index 相同时按创建时间降序
     */
    @Select("<script>" + list +
            "<where> " +
            "<if test='episodeId != null'>and e.episode_id = #{episodeId}</if> " +
            "<if test='episodeTitle != null'>and e.episode_title = #{episodeTitle}</if> " +
            "<if test='episodeInfo != null'>and e.episode_info = #{episodeInfo}</if> " +
            "<if test='episodeUrl != null'>and e.episode_url = #{episodeUrl}</if> " +
            "<if test='episodeIndex != null'>and e.episode_index = #{episodeIndex}</if> " +
            "<if test='fkChapterId != null'>and e.fk_chapter_id = #{fkChapterId}</if> " +
            "<if test='createTime != null'>and e.create_time = #{createTime}</if> " +
            "<if test='modifyTime != null'>and e.modify_time = #{modifyTime}</if> " +
            "</where> " +
            "order by e.episode_index, e.create_time desc " +
            "</script>")
    List<Episode> select(Episode condition);

    /**
     * 按章表主键查询集记录（分步调用专用SQL块）
     *
     * @param chapterId 章表主键
     * @return 查询失败时返回空List对象，结果集按 episode_index 升序，episode_index 相同时按创建时间降序
     */
    @Select(list + " where e.fk_chapter_id = #{param1} order by e.episode_index, e.create_time desc")
    List<Episode> selectByChapterId(Integer chapterId);





/*    *//**
     * 查询Chapter记录
     *
     * @param chapter Chapter实体
     * @return 多条Chapter记录，包含Episode记录
     *//*
    @Results({
            @Result(id = true, column = "chapter_id", property = "chapterId"),
            @Result(column = "chapter_id", property = "episodes",
                    many = @Many(select = "com.lyy.mapper.EpisodeMapper.selectByChapterId"))
    })
    @Select("<script>" +
            "select c.chapter_id, c.chapter_title, c.chapter_info, c.fk_video_id, " +
            "c.index_in_video, c.create_time, c.modify_time " +
            "from lesson.chapter c" +
            "<where>" +
            "<if test='chapterId != null'>and chapter_id = #{chapterId}</if>" +
            "<if test='chapterTitle != null'>and chapter_title = #{chapterTitle}</if>" +
            "<if test='chapterInfo != null'>and chapter_info = #{chapterInfo}</if>" +
            "<if test='fkVideoId != null'>and fk_video_id = #{fkVideoId}</if>" +
            "<if test='indexInVideo != null'>and index_in_video = #{indexInVideo}</if>" +
            "<if test='createTime != null'>and create_time = #{createTime}</if>" +
            "<if test='modifyTime != null'>and modify_time = #{modifyTime}</if>" +
            "</where>" +
            "order by c.index_in_video" +
            "</script>")
    List<Chapter> select(Chapter chapter);
    *//*按照chapter主键查询episode记录*//*
    @Select("select * from lesson.episode where fk_chapter_id =#{param1} order by index_in_chapter ")
    List<Episode>selectByChapter(Integer chapterId);

    *//*查询*//*
    @Select("select * from lesson.episode where episode_id=#{param1}")
    Episode selectByEpisodeId(Integer episodeId);

    *//*修改*//*
    String updateByEpisode="<script> update lesson.episode <set>"
            +"<if test='episodeTitle!=null'>episode_title=#{episodeTitle},</if>"
            +"<if test='episodeInfo!=null'>episode_info=#{episodeInfo},</if>"
            +"<if test='episodeUrl!=null'>episode_url=#{episodeUrl},</if>"
            +"<if test='fkChapterId!=null'>fk_chapter_id=#{fkChapterId},</if>"
            +"<if test='indexInChapter!=null'>index_in_chapter=#{indexInChapter},</if>"
            +"<if test='modifyTime!=null'>modify_time=#{modifyTime},</if>"
            +"</set>"
            +"where episode_id=#{episodeId}"
            +"</script>";
    @Update(updateByEpisode)
    int updateByEpisodeId(Episode episode);

    *//*添加*//*
    @Insert("insert into lesson.episode(episode_title,episode_info,episode_url,fk_chapter_id,index_in_chapter,create_time,modify_time)" +
            "value(#{episodeTitle},#{episodeInfo},#{episodeUrl},#{fkChapterId},#{indexInChapter},#{createTime},#{modifyTime})")
    @Options
    int insertEpisode(Episode episode);

    *//*删除*//*
    @Delete("delete from lesson.episode where episode_id=#{param1}")
    int deleteByepisode(Integer episodeId);*/
}
